<template>
	<view class="mainCon">
		<view class="headForm">
			<u-form :model="formData" ref="uForm" label-width="160">
				<u-form-item label="费用类别" label-width="160">
					<u-input type="select" :select-open="carCodeShow" v-model="formData.subject"
						placeholder="请选择费用" @click="carCodeShow = true"></u-input>
						<u-select mode="single-column" :list="carCodeList" v-model="carCodeShow" @confirm="carCodeConfirm"
							value-name="subject" label-name="subject"></u-select>
				</u-form-item>
				<u-form-item label="金额">
					<u-input v-model="formData.sum" type="number" />
				</u-form-item> 
				<u-form-item label="备注">
					<u-input v-model="formData.memo" />
				</u-form-item> 
			</u-form>

			<view class="btnGroup"> 
				<u-button :custom-style="customStyle" type="primary" @click="tapSubmit">支付</u-button>
			</view>
		</view>   
	</view>
</template>

<script>
	export default {
		data() {
			return {
				customStyle: {
					height: '70rpx',
				},  
				carCodeShow:false,
				carCodeList: [{subject:'靠泊费'},{subject:'岸电费'},{subject:'杂项费'}],
				isCheckAll: false, 
				flag: false,
				truckname: uni.getStorageSync("nickName"),
				formData: {
					subject:'',
					sum:'',
					memo:''
					}
			};
		},
		onLoad() {
			this.getOptions()
		},
		methods: {
			getOptions() {  
				 
			},  
			carCodeConfirm(e) { 
				this.formData.subject = e[0].value
			},     
			getProjectNum() {
				return Date.now();
			},
			getRandom(len, max) {
			      var arr = [];
			      var result = "";
			      var count = 0;
			      while (count < len) {
			        var n = Math.floor(Math.random() * max + 1);
			        if (arr.join().indexOf(n) == -1) {
			          arr.push(n);
			          count++;
			        }
			      }
			      for (let index = 0; index < arr.length; index++) {
			        result = result + arr[index];
			      }
			      return result;
			}, 
			tapSubmit() {
				if (this.flag) return
				 
				let allcount=this.formData.sum
				let subject=this.formData.subject
				if (subject == "" || allcount==0){
					return uni.showToast({
						title: '费用科目和金额必填',
						icon: 'none'
					})
					
				}
  
				this.flag = true
				let orderno="SZGJXCX"+this.getProjectNum()+this.getRandom(4,9);
				uni.request({
							url: 'https://pay.zlkj-port.com/paysys/online_pay/zlkj/pay/preorder',
							data: {
								"req_time":Date.now(),
								"version":"3.0",
								"out_org_code":"OP00000397",
								"req_data":{
								    "merchant_no":"82230604214008D",
								    "term_no":"F2034382",
								    "out_trade_no":orderno,
								    "account_type":"WECHAT",
								    "trans_type":71,
								    "total_amount":allcount*100,
								    "location_info":{
								        "request_ip":"119.45.9.131"
								    },
								    "notify_url":"",
								    "acc_busi_fields":{
								        "sub_appid":"wx8c96ae29e572f561",
								        "user_id":uni.getStorageSync("userInfo").openid,
								        "attach":""
								    }
								}
							}, 
							headers: {
							 'Content-Type': 'application/json'
						   }, 
							method: "POST", 
							success: (res) => {
								this.flag = false
								console.log(res); 
								if(res.data.msg=="验签成功"){
									let resdata=  JSON.parse(res.data.source.substr(res.data.source.indexOf("{"),res.data.source.length))
									console.log(resdata.resp_data.acc_resp_fields.time_stamp); 
									console.log(resdata.resp_data.acc_resp_fields.nonce_str); 
									console.log(resdata.resp_data.acc_resp_fields.package); 
									
									
									
									////////////////////////////////////////////
									wx.requestPayment
									(
										{
											"timeStamp": resdata.resp_data.acc_resp_fields.time_stamp,
											"nonceStr": resdata.resp_data.acc_resp_fields.nonce_str,
											"package": resdata.resp_data.acc_resp_fields.package,
											"signType": "RSA",
											"paySign":resdata.resp_data.acc_resp_fields.pay_sign,
											"success":function(res){
												console.log(res); 
												uni.showToast({
													title: '支付成功'
												}) 
												uni.request({
													url: 'https://api.zlkj-port.com/szgjapi/zlzg/A/70OlogUHZZxxZUo?ordername='+subject+'&recnam='+uni.getStorageSync("nickName")+'&orderno='+orderno+'&amount='+allcount+'&trucktaskid=0&id=0',
												    method: "get",
													success: (res) => { 
														 
													}
												})
											},
											"fail":function(res){
												uni.showToast({
													title: "支付失败2",
													icon: 'none'
												}) 
											},
											"complete":function(res){}
										}
									) 
								}else{
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									}) 
								} 
							},
							fail() {
								this.flag = false
								uni.showToast({
									title: "支付失败1",
									icon: 'none'
								}) 
							}
						})   
			}  
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-form-item {
		padding: 6rpx !important;
	}
	page {
		background-color: #F2F1F9;
	}
	.headForm {
		padding: 20rpx;
		position: fixed;
		width: 100%;
		top: 0;
		z-index: 10;
		background-color: #FFFFFF;

		
	}
.btnGroup {
			 
			margin-top: 15rpx;
		}
	.listCon {

		.listItem {
			margin-left:10px;
			padding: 20rpx;
			border-bottom: 1px solid $u-border-color;
			display: flex;
			align-items: center;

			

			.infoCon {
				
				display: flex;
				align-items: center;
				justify-content: space-between;
				.cellItem {
					display: flex;
					align-items: center;
					font-size: 28rpx;

					.label {}
				}
				.labelTag {
					background-color: #E0EDFB;
					color: #4C86E7; 
					line-height: 50rpx;
					border-radius: 25rpx;
					font-size: 26rpx;
					text-align: center;
					padding-right: 15rpx;
					padding-left: 15rpx;
				}
				.checkBox {}
			}
		}
	}
</style>
